import os
from models import *
from google.appengine.api import users



class TagManager():
  def NewTag(self,name,url,lang):
    tag = Tag()
    tag.name = name

    querylanguage = Language.all()
    querylanguage.filter('url = ', lang)
    languages = querylanguage.fetch(1)
    language = languages[0]

    tag.language = language
    index = 0
    tmpstring = ''
    letters = []
    while index < len(url):
      tmpstring += url[index]
      letters.append(tmpstring)
      index+=1
      
    tag.url = url
    tag.letters = letters
    tag.put()
    return tag

  def CheckTag(self,name,url,lang):
    querytags = Tag.all()
    querytags.filter('url = ', url)
    tags = querytags.fetch(1)
    if len(tags)==0 and name!='':
      tag = self.NewTag(name,url,lang)
    else:
      tag = tags[0]
    return tag


  def EditTag(self,key,name,url,lang):
    tag = Tag.get(key)
    index = 0
    tmpstring = ''
    letters = []
    while index < len(url):
      tmpstring += url[index]
      letters.append(tmpstring)
      index+=1

    querylanguage = Language.all()
    querylanguage.filter('url = ', lang)
    languages = querylanguage.fetch(1)
    language = languages[0]
    tag.language = language
    tag.name = name
    tag.url = url
    tag.letters = letters
    tag.put()
    return tag


  def DeleteTag(self,key):
    tag=Tag.get(key)
    tag.delete()
    return True


class TermManager():
  
  def NewTerm(self,word,prefix,translate,translate2,translate3,url,level,tags,language,termtype,attributes):
      term = Term()
      term.word = word
      term.prefix =  prefix
      term.translate = translate
      term.translate2 = translate2
      term.translate3 = translate3
      term.url = url
      term.level = level
      term.tags = tags
      term.language = language
      term.termtype = termtype
      term.attributes = attributes
      term.autonumeric = self.getAutonumeric(language.url)
      term.put()
      self.incrementAutonumeric(language.url)
      return term


  def EditTerm(self,key,word,prefix,translate,translate2,translate3,url,level,tags,language,termtype,attributes):
    term = Term.get(key)
    term.word = word
    term.prefix =  prefix
    term.translate = translate
    term.translate2 = translate2
    term.translate3 = translate3
    term.url = url
    term.level = level
    term.tags = tags
    term.language = language
    term.termtype = termtype
    term.attributes = attributes
    term.put()
    
    return term

  def DeleteTerm(self,key):
    term = Term.get(key)
    lang = term.language.url
    term.delete()
    self.DecrementAutonumeric(lang)
    return True
  
  
  def getAutonumeric(self,lang):
    if lang == 'de':
      configuration = Configuration.get('agpoZWFkZXJsaW5lchULEg1Db25maWd1cmF0aW9uGMnvNww')
    else:
      configuration = Configuration.get('agpoZWFkZXJsaW5lchULEg1Db25maWd1cmF0aW9uGLH3Nww')
    
    return int(configuration.value)
  
  
  def incrementAutonumeric(self,lang):
    if lang == 'de':
      configuration = Configuration.get('agpoZWFkZXJsaW5lchULEg1Db25maWd1cmF0aW9uGMnvNww')
    else:
      configuration = Configuration.get('agpoZWFkZXJsaW5lchULEg1Db25maWd1cmF0aW9uGLH3Nww')
    
    value = int(configuration.value) + 1
    configuration.value = str(value)
    configuration.put()
    
    return value


  def DecrementAutonumeric(self,lang):
    if lang == 'de':
      configuration = Configuration.get('agpoZWFkZXJsaW5lchULEg1Db25maWd1cmF0aW9uGMnvNww')
    else:
      configuration = Configuration.get('agpoZWFkZXJsaW5lchULEg1Db25maWd1cmF0aW9uGLH3Nww')
    
    value = int(configuration.value) - 1
    configuration.value = str(value)
    configuration.put()
    
    return value

